package com.example.stringbufferlogger.Utils;
import com.example.stringbufferlogger.VO.ComparisonResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Component
public class XmlJsonConverter {

    private final XmlMapper xmlMapper;
    private final ObjectMapper jsonMapper;

    /**
     * 
     * @param oldRes
     * @param newRes
     * @param ignoredFields
     * @return
     */
    public static ComparisonResult compareXml(String oldRes,String newRes,Set<String> ignoredFields){
        XmlJsonConverter xmlJsonConverter = new XmlJsonConverter();
        String json = xmlJsonConverter.convertXmlToJson(oldRes,true);
        String json2 = xmlJsonConverter.convertXmlToJson(newRes,true);

        return JsonComparator.compareJson(json, json2, ignoredFields);
    }

    public XmlJsonConverter() {
        this.xmlMapper = new XmlMapper();
        this.jsonMapper = new ObjectMapper();
    }

    /**
     * 将XML字符串转换为格式化的JSON字符串
     * @param xml XML格式字符串
     * @return 格式化后的JSON字符串
     */
    public String convertXmlToJson(String xml)  {
        try{
            // 1. 将XML解析为JsonNode
            JsonNode xmlNode = xmlMapper.readTree(xml.getBytes());

            // 2. 转换为JSON字符串
            return jsonMapper.writerWithDefaultPrettyPrinter()
                    .writeValueAsString(xmlNode);
        }catch (Exception e){
            return null;
        }

    }

    /**
     * 带注释过滤的转换方法
     * @param xml XML格式字符串
     * @param removeComments 是否移除注释
     */
    public String convertXmlToJson(String xml, boolean removeComments) {
        if (removeComments) {
            xml = xml.replaceAll("<!--.*?-->", "");
        }
        return convertXmlToJson(xml);
    }

    public static void main(String[] args) throws Exception {
        String xml = "<Policy>" +
                "    <!-- 保单信息 -->" +
                "    <PolicyInfo>" +
                "        <PolicyNumber>123456</PolicyNumber>" +
                "        <PolicyDate>2024-10-01</PolicyDate>" +
                "        <PolicyType>Health Insurance</PolicyType>" +
                "    </PolicyInfo>" +
                "    <!-- 险种信息 -->" +
                "    <InsuranceType>" +
                "        <TypeName>重大疾病险</TypeName>" +
                "        <CoverageAmount>500000</CoverageAmount>" +
                "        <Duration>终身</Duration>" +
                "    </InsuranceType>" +
                "    <!-- 被保人信息 -->" +
                "    <Insured>" +
                "        <InsuredName>张三</InsuredName>" +
                "        <InsuredAge>35</InsuredAge>" +
                "        <InsuredGender>男</InsuredGender>" +
                "        <InsuredBirthDate>1989-05-10</InsuredBirthDate>" +
                "        <!-- 受益人信息 -->" +
                "        <Beneficiaries>" +
                "            <Beneficiary>" +
                "                <BeneficiaryName>张小三</BeneficiaryName>" +
                "                <Relationship>子女</Relationship>" +
                "                <BeneficiaryGender>男</BeneficiaryGender>" +
                "                <BeneficiaryPercentage>50</BeneficiaryPercentage>" +
                "            </Beneficiary>" +
                "            <Beneficiary>" +
                "                <BeneficiaryName>李四</BeneficiaryName>" +
                "                <Relationship>配偶</Relationship>" +
                "                <BeneficiaryGender>女</BeneficiaryGender>" +
                "                <BeneficiaryPercentage>50</BeneficiaryPercentage>" +
                "            </Beneficiary>" +
                "            <Beneficiary>" +
                "                <BeneficiaryName>张三</BeneficiaryName>" +
                "                <Relationship>子女</Relationship>" +
                "                <BeneficiaryGender>男</BeneficiaryGender>" +
                "                <BeneficiaryPercentage>50</BeneficiaryPercentage>" +
                "            </Beneficiary>" +
                "        </Beneficiaries>" +
                "    </Insured>" +
                "</Policy>";

        String xml2 = "<Policy>" +
                "    <!-- 保单信息 -->" +
                "    <PolicyInfo>" +
                "        <PolicyNumber>123456</PolicyNumber>" +
                "        <PolicyDate>2024-10-01</PolicyDate>" +
                "        <PolicyType>Health Insurance</PolicyType>" +
                "    </PolicyInfo>" +
                "    <!-- 险种信息 -->" +
                "    <InsuranceType>" +
                "        <TypeName>重大1疾病险</TypeName>" +
                "        <CoverageAmount>500000</CoverageAmount>" +
                "        <Duration>终身</Duration>" +
                "    </InsuranceType>" +
                "    <!-- 被保人信息 -->" +
                "    <Insured>" +
                "        <InsuredName>张1三</InsuredName>" +
                "        <InsuredAge>35</InsuredAge>" +
                "        <InsuredGender>男</InsuredGender>" +
                "        <InsuredBirthDate>1989-05-10</InsuredBirthDate>" +
                "        <!-- 受益人信息 -->" +
                "        <Beneficiaries>" +
                "            <Beneficiary>" +
                "                <BeneficiaryName>张小三</BeneficiaryName>" +
                "                <Relationship>子女</Relationship>" +
                "                <BeneficiaryGender>男</BeneficiaryGender>" +
                "                <BeneficiaryPercentage>50</BeneficiaryPercentage>" +
                "            </Beneficiary>" +
                "            <Beneficiary>" +
                "                <BeneficiaryName>李四</BeneficiaryName>" +
                "                <Relationship>配偶</Relationship>" +
                "                <BeneficiaryGender>女</BeneficiaryGender>" +
                "                <BeneficiaryPercentage>50</BeneficiaryPercentage>" +
                "            </Beneficiary>" +
                "        </Beneficiaries>" +
                "    </Insured>" +
                "</Policy>";

        Set<String> ignoredFields = new HashSet<>();
        ignoredFields.add("age");
        ignoredFields.add("street");
        ComparisonResult result = compareXml(xml,xml2,ignoredFields);

        System.out.println(result.getDifferencesStr());

    }
}